<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }

      .w {
        width: 1200px;
        margin: 20px auto;
      }

      .container {
        width: 1200px;
        height: 600px;
        margin: 20px auto;
        /* background-color: pink; */
      }

      .container .small {
        /* padding: 30px; */
        position: relative;
        float: left;
        border: 1px solid #ccc;
        margin-right: 10px;
      }

      .container .small img {
        display: block;
        /* border: 2px solid red; */
      }

      .container .small .mask {
        position: absolute;
        top: 0px;
        left: 0px;
        width: 300px;
        height: 300px;
        background-color: #ffefa7;
        opacity: 0.7;
        cursor: move;
      }

      .container .big {
        position: relative;
        float: left;
        width: 600px;
        height: 500px;
        /* border: 10px solid skyblue; */
        overflow: hidden;
      }

      .container .big .bigImg {
        /* border: 5px solid green; */
        position: absolute;
      }

      .hide {
        display: none;
      }
    </style>
  </head>

  <body>
    <div>
      <div class="container">
        <div class="small">
          <img src="./img/small.jpg" alt="" />
          <!-- 遮盖 -->
          <div class="mask hide"></div>
        </div>
        <div class="big hide">
          <img src="./img/big.jpg" class="bigImg" alt="" />
        </div>
      </div>
    </div>
    <script>
      var smallBox = document.querySelector('.small');
      var mask = document.querySelector('.mask');
      var bigBox = document.querySelector('.big');
      var bigImg = document.querySelector('.bigImg');
      console.log(bigBox.clientWidth);

      console.log(getComputedStyle(bigBox, null).width);

      console.log(bigImg);
      //鼠标移到小图片上面，遮盖和大图片展示
      smallBox.addEventListener('mouseenter', function () {
        mask.classList.remove('hide');
        bigBox.classList.remove('hide');
      });
      smallBox.addEventListener('mouseleave', function () {
        mask.classList.add('hide');
        bigBox.classList.add('hide');
      });
      //给小盒子绑定悬浮事件
      smallBox.addEventListener('mousemove', function (event) {
        //遮盖移动的距离
        var maskMoveLeft =
          event.clientX -
          mask.offsetWidth / 2 -
          smallBox.getBoundingClientRect().left;
        var maskMoveTop =
          event.clientY -
          mask.offsetHeight / 2 -
          smallBox.getBoundingClientRect().top;
        if (maskMoveLeft < 0) {
          maskMoveLeft = 0;
        }
        if (maskMoveTop < 0) {
          maskMoveTop = 0;
        }
        //遮盖box可移动的最大距离
        var maskMaxMoveLeft = smallBox.clientWidth - mask.offsetWidth;
        var maskMaxMoveTop = smallBox.clientHeight - mask.offsetHeight;
        if (maskMaxMoveTop < maskMoveTop) {
          maskMoveTop = maskMaxMoveTop;
        }
        if (maskMoveLeft > maskMaxMoveLeft) {
          maskMoveLeft = maskMaxMoveLeft;
        }
        mask.style.left = maskMoveLeft + 'px';
        mask.style.top = maskMoveTop + 'px';
        //大图的偏移量 等比例缩放
        // 遮盖移动的距离 /遮盖最大移动距离 = 大图移动的距离/ 大图最大移动的距离
        var bigImgMaxLeft = bigImg.clientWidth - bigBox.offsetWidth;
        console.log(bigImgMaxLeft);
        var bigImgMaxTop = bigImg.clientHeight - bigBox.offsetHeight;
        //大图移动距离 =遮盖移动距离*大图最大的移动距离 /遮盖最大移动距离
        var bigImgMoveLeft = (maskMoveLeft * bigImgMaxLeft) / maskMaxMoveLeft;
        var bigImgMoveTop = (maskMoveTop * bigImgMaxTop) / maskMaxMoveTop;
        bigImg.style.left = -bigImgMoveLeft + 'px';
        bigImg.style.top = -bigImgMoveTop + 'px';
      });
    </script>
  </body>
</html>
